# 构建阶段
FROM maven:3.8.4-openjdk-17 as build-stage

WORKDIR /app

# 复制Maven配置文件
COPY settings.xml /usr/share/maven/conf/settings.xml

# 复制pom文件
COPY pom.xml .
COPY continew-common/pom.xml continew-common/
COPY continew-extension/pom.xml continew-extension/
COPY continew-extension/continew-extension-schedule-server/pom.xml continew-extension/continew-extension-schedule-server/
COPY continew-module-system/pom.xml continew-module-system/
COPY continew-plugin/pom.xml continew-plugin/
COPY continew-plugin/continew-plugin-generator/pom.xml continew-plugin/continew-plugin-generator/
COPY continew-plugin/continew-plugin-open/pom.xml continew-plugin/continew-plugin-open/
COPY continew-plugin/continew-plugin-schedule/pom.xml continew-plugin/continew-plugin-schedule/
COPY continew-webapi/pom.xml continew-webapi/

# 复制源代码
COPY . .

# 构建项目
RUN mvn clean install -pl continew-webapi -am -DskipTests

# 运行阶段
FROM eclipse-temurin:17-jre

WORKDIR /app

# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone

# 检查构建结果
RUN echo "Checking build stage files:"
COPY --from=build-stage /app/continew-webapi/target/app/ ./target-check/
RUN ls -la ./target-check/ && ls -la ./target-check/bin/

# 复制jar文件和依赖
RUN cp ./target-check/bin/*.jar ./app.jar

# 创建日志目录
RUN mkdir -p /app/logs

# 检查jar文件是否存在
RUN ls -la /app/

EXPOSE 8080



# 调试脚本
RUN echo '#!/bin/bash' > /app/debug.sh && \
    echo 'echo "=== Environment Variables ==="' >> /app/debug.sh && \
    echo 'env | grep -E "(DB_|REDIS_|SPRING_)" || echo "No matching env vars found"' >> /app/debug.sh && \
    echo 'echo "=== Config Files ==="' >> /app/debug.sh && \
    echo 'find /app -name "*.yml" -o -name "*.yaml" -o -name "*.properties" | head -10' >> /app/debug.sh && \
    echo 'echo "=== Starting Application ==="' >> /app/debug.sh && \
    echo 'java -cp "/app/app.jar:/app/target-check/lib/*:/app/target-check/config" -Dspring.profiles.active=prod top.continew.admin.ContiNewAdminApplication' >> /app/debug.sh && \
    chmod +x /app/debug.sh

ENTRYPOINT ["/app/debug.sh"]